﻿using System.Collections.Generic;
using Centido.Core.Dto;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Centido.Core.Test
{
    /// <summary>
    ///   Testing the data transfer object for a user.
    /// </summary>
    [TestClass]
    public class UserDtoTest : GenericDtoTest
    {
        /// <summary>
        ///   The current domain name.
        /// </summary>
        public const string DomainName = "FullAdminRights";

        /// <summary>
        ///   The current domain.
        /// </summary>
        private Domain _domain;


        /// <summary>
        /// Test initialiaze
        /// </summary>
        [TestInitialize]
        public void Initialize()
        {
            _domain = Domain.Build(DomainConfiguration.Load(DomainName));

            _domain.DeleteAll();
        }


        /// <summary>
        /// Test clean up
        /// </summary>
        [TestCleanup]
        public void CleanUp()
        {
            _domain.DeleteAll();
        }


        /// <summary>
        ///   Retrieves the name of the data transfer object xml tag.
        /// </summary>
        /// <returns></returns>
        protected override string GetXmlTag()
        {
            return UserDto.XmlTag;
        }


        /// <summary>
        ///   Retrives the current xml schema version.
        /// </summary>
        /// <returns></returns>
        protected override string GetCurrentXmlSchemaVersion()
        {
            return UserDto.CurrentXmlSchemaVersion;
        }


        /// <summary>
        ///   Creates a new data transfer object.
        /// </summary>
        /// <returns></returns>
        protected override DataTransferObject CreateDto()
        {
            return new UserDto
            {
                Name = StringProperty.RandomIdentifier(),
                DisplayName = "The user display name",
                CanLogon = false,
                CanChangeSchema = true,
                CanChangeSecurity = true
            };
        }


        /// <summary>
        ///   Retrieves the list of data transfer object fields.
        /// </summary>
        /// <returns></returns>
        protected override IEnumerable<DtoFieldInfo> GetFields()
        {
            yield return new DtoFieldInfo
            {
                Name = "Name",
                XmlName = UserDto.NameAttribute,
                IsRequired = true,
                FieldType = DtoFieldType.String,
                SampleValue = "user_name",
                IsCopiedFromDto = false
            };

            yield return new DtoFieldInfo
            {
                Name = "DisplayName",
                XmlName = UserDto.DisplayNameAttribute,
                IsRequired = false,
                FieldType = DtoFieldType.String,
                SampleValue = "Display name"
            };

            yield return new DtoFieldInfo
            {
                Name = "CanLogon",
                XmlName = UserDto.CanLogonAttribute,
                IsRequired = false,
                FieldType = DtoFieldType.Boolean,
                SampleValue = false
            };

            yield return new DtoFieldInfo
            {
                Name = "CanChangeSchema",
                XmlName = UserDto.CanChangeSchemaAttribute,
                IsRequired = false,
                FieldType = DtoFieldType.Boolean,
                SampleValue = true
            };

            yield return new DtoFieldInfo
            {
                Name = "CanChangeSecurity",
                XmlName = UserDto.CanChangeSecurityAttribute,
                IsRequired = false,
                FieldType = DtoFieldType.Boolean,
                SampleValue = true
            };
        }


        /// <summary>
        ///   Copies the properties of the data transfer object to the corresponding object.
        /// </summary>
        /// <param name="dto">The data transfer object.</param>
        /// <returns>Returns the corresponding object after copying.</returns>
        protected override object CopyFrom(DataTransferObject dto)
        {
            var user = _domain.AddUser(StringProperty.RandomIdentifier(), StringProperty.RandomPassword());

            ((UserDto)dto).CopyTo(user);

            return user;
        }


        /// <summary>
        ///   Creates a data transfer object from the correpsonding object.
        /// </summary>
        /// <param name="dto">The created data transfer object.</param>
        /// <param name="initialObject">The initial object.</param>
        protected override void CreateDtoFromObject(out DataTransferObject dto, out object initialObject)
        {
            var user = _domain.AddUser(StringProperty.RandomIdentifier(), StringProperty.RandomPassword());

            dto = UserDto.CreateFrom(user);
            initialObject = user;
        }
    }
}
